package org.jsets.fastboot.database;

/**
 * TODO
 *
 * @author wangjie (https://github.com/wj596)
 * @date 2022.01.06 17:51
 * @since 0.1
 */
public abstract class OracleTypes {

    public static final String VARCHAR = "VARCHAR";// 	java.lang.String 	12
    public static final String VARCHAR2 	= "VARCHAR2";// 	java.lang.String 	1
    public static final String CHAR 	= "CHAR";// 	java.lang.String
    public static final String BLOB 	= "BLOB";// 	byte[]
    public static final String CLOB 	= "CLOB";// 	byte[]
    public static final String NUMBER 	= "NUMBER";//
    public static final String NUMERIC 	= "NUMERIC";//
    public static final String DATE 	= "DATE";//
    public static final String TIMESTAMP 	= "TIMESTAMP";//

    
    public static String toJavaType(String oracleType, int columnSize, int decimalDigits) {
		String upper = oracleType.toUpperCase();
		switch (upper) {
		case OracleTypes.CHAR:
		case OracleTypes.VARCHAR:
		case OracleTypes.VARCHAR2:
			return JavaTypes.STRING;

		case OracleTypes.DATE:
		case OracleTypes.TIMESTAMP:
			return JavaTypes.DATE;

		case OracleTypes.BLOB:
		case OracleTypes.CLOB:
			return JavaTypes.BYTES;

		case OracleTypes.NUMBER:
		case OracleTypes.NUMERIC:
			if (decimalDigits == 0) {
				if (columnSize == 1)
					return JavaTypes.BOOLEAN;
				if (columnSize > 1 && columnSize <= 10)
					return JavaTypes.INTEGER;
				if (columnSize > 10 && columnSize <= 18)
					return JavaTypes.LONG;
				if (columnSize > 18)
					return JavaTypes.BIG_DECIMAL;
			} else {
				if (decimalDigits <= 8)
					return JavaTypes.FLOAT;
				return JavaTypes.DOUBLE;
			}
		}
		return JavaTypes.STRING;
	}

	public static String fromJavaType(String javaType) {
		switch (javaType) {
		case JavaTypes.STRING:
			return OracleTypes.VARCHAR2;
		case JavaTypes.DATE:
			return OracleTypes.DATE;
		case JavaTypes.TIMESTAMP:
			return OracleTypes.TIMESTAMP;
		case JavaTypes.INTEGER:
			return OracleTypes.NUMBER;
		case JavaTypes.LONG:
			return OracleTypes.NUMBER;
		case JavaTypes.FLOAT:
			return OracleTypes.NUMBER;
		case JavaTypes.DOUBLE:
			return OracleTypes.NUMBER;
		case JavaTypes.BOOLEAN:
			return OracleTypes.NUMBER;
		case JavaTypes.BIG_DECIMAL:
			return OracleTypes.NUMBER;
		case JavaTypes.BYTES:
			return OracleTypes.BLOB;
		}
		return OracleTypes.VARCHAR2;
	}
    
    
}
